package com.digitaldreamers.spaceshooter.scene;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;

import com.digitaldreamers.framework.scene.SceneManager;
import com.digitaldreamers.framework.scene.background.AutoVerticalParallaxBackground;
import com.digitaldreamers.framework.scene.background.VerticalParallaxBackground.VerticalParallaxEntity;
import com.digitaldreamers.spaceshooter.Constants;

public class MainMenuScene extends BaseScene {
	private Sprite mBackground;
	private Text mGameTitle;
	private Text mPlayMenu;
	private Text mOptionMenu;

	@Override
	public Scene onLoadingSceneLoadAndShown() {
		return null;
	}

	@Override
	public void onLoadingSceneUnloadAndHidden() {
		
	}

	@Override
	public void onLoadScene() {
		this.mResourceManager.loadMainMenuSceneResources();
		this.createBackground();
		this.createGameTitle();
		this.createMenu();
	}

	@Override
	public void onShowScene() {
		
	}

	@Override
	public void onHideScene() {
		
	}

	@Override
	public void onUnloadScene() {
		super.onUnloadScene();
		this.mResourceManager.unloadMainMenuSceneResources();
	}
	
	@SuppressWarnings("deprecation")
	private void createBackground() {
		this.mBackground = new Sprite(0, 0, this.mResourceManager.mMainMenuBackgroundTextureRegion, this.mVBOM);
		float background1ScaledWidth = this.mBackground.getWidthScaled();
		float background1ScaledHeight = this.mBackground.getHeightScaled();
		this.mBackground.setPosition(background1ScaledWidth / 2, background1ScaledHeight / 2);
		
		AutoVerticalParallaxBackground parallaxBackground = new AutoVerticalParallaxBackground(0f, 0f, 0f, Constants.PARALLAX_SCROLL_SPEED__NORMAL);
		parallaxBackground.attachVerticalParallaxEntity(new VerticalParallaxEntity(Constants.PARALLAX_FACTOR, this.mBackground));
		this.setBackgroundEnabled(true);
		this.setBackground(parallaxBackground);
	}
	
	private void createGameTitle() {
		this.mGameTitle = new Text(0, 0, this.mResourceManager.mMoonHouseFont, Constants.GAME_TITLE, this.mVBOM);
		this.mGameTitle.setPosition(this.mCameraMiddleX, this.mCameraMiddleY + 100);
		this.attachChild(this.mGameTitle);
		this.mGameTitle.setScale(this.mGameTitle.getScaleX() * 1.5f, this.mGameTitle.getScaleY() * 1.5f);
	}
	
	private void createMenu() {
		this.mPlayMenu = new Text(0, 0, this.mResourceManager.mMoonHouseFont, Constants.MAIN_MENU__PLAY, this.mVBOM) {
			@Override
			public boolean onAreaTouched(TouchEvent sceneTouchEvent, float touchAreaLocalX, float touchAreaLocalY) {
				if(sceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
					SceneManager.getInstance().showScene(new GameScene());
				}
				return true;
			}
		};
		this.mPlayMenu.setPosition(this.mCameraMiddleX, this.mCameraMiddleY);
		this.registerTouchArea(this.mPlayMenu);
		this.attachChild(this.mPlayMenu);
		
		this.mOptionMenu = new Text(0, 0, this.mResourceManager.mMoonHouseFont, Constants.MAIN_MENU__OPTIONS, this.mVBOM);
		this.mOptionMenu.setPosition(this.mCameraMiddleX, this.mCameraMiddleY - 50);
		this.attachChild(this.mOptionMenu);
	}
}
